<%@ page contentType="text/html; charset=utf-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript">
var v_typename;
var v_typecode = "0";
$(document).ready(function() { 
		$('#tree_sitetype2').tree({
			url: '<c:url value="/system/site/sitetype/findSiteTypeTree.do"/>',
			onBeforeExpand: function(node) {
				var url = '<c:url value="/system/site/sitetype/findSiteTypeTree.do"/>?parentcode='+ node.id;
				$('#tree_sitetype2').tree('options').url = url;
			},
			onLoadSuccess: function(node, data) {
				if($('#tree_sitetype2').tree('getRoot')!=null){
					$('#tree_sitetype2').tree('expand', $('#tree_sitetype2').tree('getRoot').target);
				}
			},
			onSelect: function(node) {
				sitereload(node.id,node.text);
				if(node.id!=null&&node.id!=''&&node.id!='0')
					$('#siteCenter').panel('setTitle','网站库管理-'+node.text);
				else
					$('#siteCenter').panel('setTitle','网站库管理');
			},
			onDblClick: function(node) {
	        	/*展开或折叠树节点*/
				$(this).tree('toggle', node.target);
	        }
		});
	
		
	$('#siteList').datagrid({
		fit: true,
		title:'',
		nowrap: false,
		striped: true,
		singleSelect: true,
		url: '<c:url value="/system/site/site/findSitePage.do"/>',
		queryParams:{typecode:'0'},
		sortName: 'addtime',
		sortOrder: 'desc',
		idField: 'id',
		frozenColumns: [[
			{field:'ck',checkbox:true},
			{field:'TYPENAME',title:'分类代码',width:150,sortable:true},
		]],
		columns: [[
		    {field:'URL',title:'网址',width:350,sortable:true},
		    {field:'ISABNORMAL',title:'异常网站',width:100,formatter:function(value){
				return value=="1"?"<font color=red>异常</font>":"正常";
			}},
			{field:'ADDTIME',title:'添加时间',width:130,sortable:true}
		]],
		toolbar: [{
			text: '新增',
			iconCls: 'icon-add',
			handler: function(){
				addSite(1);
			}
		},{
			text: '删除',
			iconCls: 'icon-remove',
			handler: function(){
				delSite();
			}
		},{
			id: 'modify',
			text: '修改',
			iconCls: 'icon-edit',
			handler: function(){
				addSite(2);
			}
		}],
		pageSize: 20,
		pagination: true,
		rownumbers: true,
		onClickRow:function(rowIndex, rowData) {
			$('#siteList').datagrid('clearSelections').datagrid('selectRow',rowIndex);	
		}
	});
});

function sitereload(ptypecode,ptypename){
	v_typename = ptypename;
	v_typecode = ptypecode;
	$('#siteList').datagrid('getPager').pagination({pageNumber:1});
	var queryParams = $('#siteList').datagrid('options').queryParams;
	queryParams.typecode=ptypecode;
	$('#siteList').datagrid('options').queryParams = queryParams;
	//$('#siteList').datagrid('options').url = '<c:url value="/system/site/site/findSitePage.do"/>?typecode='+ptypecode;
	$('#siteList').datagrid('clearSelections').datagrid('load');
}

function addSite(flag){
	var url = '<c:url value="/system/site/site/siteadd.do"/>';
	var title;
	if(flag==1){
		title = '网站库添加';
		if(v_typecode=="0"){
			$.messager.alert('提示', "请选择网站分类" , 'info');
			return;
		}
	}else{
		title = '网站库修改'; 
		var update = $('#siteList').datagrid('getSelected');
		url = url +'?id='+update.ID;
	}
	$('#dlgAddSite').dialog('close').remove();
	$('body').append('<div id="dlgAddSite" class="easyui-dialog" closed="true"></div>');
	$('#dlgAddSite').dialog({
		title:title,
		modal:true,
		singleSelect:true,
		closable:true,
		draggable:false,
		width:350,
		height:180,
		toolbar:[{
			text:'保存',
			iconCls:'icon-save',
			handler:function(){
				saveSite();
			}
		},{
			text:'关闭',
			iconCls:'icon-cancel',
			handler:function(){
				$('#dlgAddSite').dialog('close');
			}
		}]
	}).dialog('open').dialog('refresh',url);
}


function saveSite(){
	if ($('#frmSite').form('validate')){
		var data = $('#frmSite').serialize();
		$.ajax({
			url: '<c:url value="/system/site/site/saveSite.do"/>',
			type: 'POST',
			data: data,
			beforeSend: function(XmlHttpRequest, textStatus, errorThrown) {
				$.messager.progress({
					title:'提示',
					msg:'正在执行数据操作，请稍待...'
				});
			},
			success: function(result) {
				$.messager.progress('close');
				if(result.success){
					$('#dlgAddSite').dialog('close').remove();
					$('#siteList').datagrid('clearSelections').datagrid('reload');
					$.messager.alert('提示', result.data , 'info');
					//v_typecode = "0";
					//v_typename = "网站库分类";	
				}
				else{
					$.messager.alert('错误', result.data , 'error');
				}
			}
		});
	}
}



function delSite(){
	var row = $('#siteList').datagrid('getSelected');
	if(row == null){
		$.messager.alert('错误', '请选择您要删除的数据！', 'error'); 
		return;
	}
	else {
		$.messager.confirm('警告', '您确认删除么?', function(r){
			if(r){
				$.ajax({
					url: '<c:url value="/system/site/site/delSite.do"/>',
					type: 'POST',
					data: 'id='+row.ID,
					beforeSend: function(XmlHttpRequest, textStatus, errorThrown) {
						$.messager.progress({
							title:'提示',
							msg:'正在执行数据操作，请稍待...'
						});
					},
					success: function(result) {
						$.messager.progress('close');
						if(!result.success) {
							$.messager.alert('错误', result.data , 'error');
						}
						else {
							$.messager.alert('提示', result.data , 'info');
							$("#siteList").datagrid('clearSelections').datagrid('reload');
						}
					}
				});
			}
		});
	}
}
</script>


<div class="easyui-layout" fit="true" style="background:#ccc;">
	<div region="west" split="true" title="网站库分类"style="width:200px;padding:0px;">
			<ul id="tree_sitetype2"></ul>
	</div>
	<div id="siteCenter" region="center" title="网站库管理"> 
		<table id="siteList" border="false" cellpadding="0"  cellspacing="0"> </table>
	</div>
</div>
